package com.hsgene.message.config;

import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.hsgene.message.service.impl.MQMessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author wxf
 * @date 2018/10/29 11:54
 **/
@Configuration
public class MQConsumer extends MQAkSk {

    @Value("${receive.topic}")
    //@Value("${send.topic}")
    private String receiveTopic;
    @Value("${receive.consumer}")
    private String receiveConsumer;

    @Bean
    public Consumer getConsumer() {
        Properties consumerProperties = new Properties();
        //consumerProperties.put(PropertyKeyConst.ConsumerId, "CID_YINZIGENE_GLAZE_TEST");
        consumerProperties.put(PropertyKeyConst.ConsumerId, receiveConsumer);
        consumerProperties.put(PropertyKeyConst.AccessKey, accessKeyID);
        consumerProperties.put(PropertyKeyConst.SecretKey, accessKeySecret);
        consumerProperties.put(PropertyKeyConst.ONSAddr, address);
        Consumer consumer = ONSFactory.createConsumer(consumerProperties);
        consumer.subscribe(receiveTopic, "*", getMQMessageListener());
        consumer.start();
        System.out.println("consumer启动");
        return consumer;
    }

    @Bean
    public MQMessageListener getMQMessageListener(){
        return new MQMessageListener();
    }
}
